package org.dreamwork.jasmine2.sample;

import org.dreamwork.jasmine2.engine.Browser;
import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.events.IChangeListener;
import org.dreamwork.jasmine2.events.IClickListener;
import org.dreamwork.jasmine2.events.IValidateListener;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.jasmine2.web.controls.Button;
import org.dreamwork.jasmine2.web.controls.DropdownList;
import org.dreamwork.jasmine2.web.controls.Page;
import org.dreamwork.jasmine2.web.controls.TextBox;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 12-3-5
 * Time: 下午5:45
 */
public class UUU extends Page {
    protected TextBox text1;
    protected Button button1;
    protected DropdownList s1;

    protected Map<String, Object> map = new HashMap<String, Object> ();
    protected List<Map<String, Object>> list = new ArrayList<Map<String, Object>> ();

    protected Browser browser;

    public UUU () {
        map.put ("a", "aaa");
        map.put ("b", "bbb");
        map.put ("c", "ccc");

        for (int i = 0; i < 3; i ++) {
            Map<String, Object> m = new HashMap<String, Object> ();
            m.put ("key", i);
            m.put ("value", "value" + i);
            list.add (m);
        }
    }

    @Override
    public void onPagePreload (Page page) throws EventException {
        text1.addValidateListener (new IValidateListener() {
            public boolean onValidate (IWebControl sender) {
                if (isPostback () && !"abc".equalsIgnoreCase (text1.getText ())) {
                    text1.setText ("error");
                    return false;
                }
                return true;
            }
        });

        button1.addClickListener (new IClickListener() {
            public void onClick (IWebControl sender) throws EventException {
                text1.setText ("success");
            }
        });

        ((DropdownList) page.findControl ("s1")).addChangeListener (new IChangeListener() {
            public void onChange (IWebControl sender, String attrName, Object oldValue, Object newVlaue) throws EventException {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });
    }

    @Override
    public void onPageLoad (Page page) throws EventException {
        super.onPageLoad (page);
    }

    @Override
    public void onPageLoadCompleted (Page page) throws EventException {
        super.onPageLoadCompleted (page);

        browser = context.getBrowser ();

        try {
            this.dataBind ();
        } catch (Throwable throwable) {
            throwable.printStackTrace ();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
